package edu.scau.divineplace.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.scau.divineplace.entity.dos.BanWordDO;
import edu.scau.divineplace.entity.dos.CommentForumDO;
import edu.scau.divineplace.entity.query.PageQuery;
import edu.scau.divineplace.entity.vo.banword.BanWordVO;
import edu.scau.divineplace.util.BeanConvertUtils;
import edu.scau.divineplace.util.PageQueryUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author: x1aolone
 * @Date: 2021/2/9 14:59
 */
@Service
public class BanWordService {

    @Autowired
    private ImportMapperService importMapperService;

    /**
     * 获取违禁词列表
     * @return
     */
    public List<BanWordVO> list(String word, PageQuery pageQuery) {
        PageQueryUtil.fillAllIfAbsent(pageQuery);
        Page<BanWordDO> banWordPage = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        LambdaQueryWrapper<BanWordDO> wrapper = Wrappers.<BanWordDO>lambdaQuery()
                .orderByDesc(BanWordDO::getUpdateTime)
                .orderByDesc(BanWordDO::getCreateTime);
        if (word != null) {
            wrapper.like(BanWordDO::getWord, word);
        }
        List<BanWordDO> DOS = importMapperService.banWordMapper.selectPage(banWordPage, wrapper).getRecords();
        List<BanWordVO> VOS = BeanConvertUtils.DOs2BOs(DOS, BanWordVO.class);
        return VOS;
    }

    /**
     * 新增违禁词
     * @param word
     * @return
     */
    public int upload(String word) {
        BanWordDO DO = new BanWordDO();
        DO.setWord(word);
        int result = importMapperService.banWordMapper.insert(DO);
        return result;
    }

    /**
     * 根据id修改违禁词
     * @param id
     * @param word
     * @return
     */
    public int update(Long id, String word) {
        LambdaUpdateWrapper<BanWordDO> wrapper = Wrappers.<BanWordDO>lambdaUpdate()
                .eq(BanWordDO::getId, id)
                .set(BanWordDO::getWord, word);
        int result = importMapperService.banWordMapper.update(new BanWordDO(), wrapper);
        return result;
    }

    /**
     * 根据id删除违禁词
     * @param id
     * @return
     */
    public int delete(Long id) {
        int result = importMapperService.banWordMapper.deleteById(id);
        return result;
    }

}
